import {
    findInApply,
    getInGoodWithInApplyId,
    approveInApply,
} from "../../../api/afftersale/refacin";

let inApplyApprove = {
    data() {
        return {
            //搜索
            search: {
                currentPage: 1,
                pageSize: 5,

                idoCode: "",//返厂入库单号
                createBy: "",//制单人
                status: "",//返厂出入库标志，0 未入库， 1 已入库
                approveStatus: "",//审核标志，0 审核不通过；1 未审核；2 已审核

                //创建时间
                startTime: "",
                endTime: "",

                //审核时间
                appStartTime: "",
                appEndTime: "",
            },
            //日期组件
            createTime: [],
            appTime: [],

            //入库申请列表
            applyList: [],

            //分页
            currentPage: 1,
            pageSize: 5,
            total: 0,

            dialogTableVisible: false, //模态框可见性
            type: "",//0 审核； 1 审核跟踪

            //审核信息
            approverTime: "",
            approverComments: "",
            approveStatus: "",
            approver: "",//审核人
            formatApproveTime: "",

            //入库单信息
            comments: "",
            refacLocation: "",
            idoCode: "",
            createBy: "",
            goodList: [],

            //提交信息
            approveInfo: {
                id: "",
                approveStatus: "",
                approverComments: "",
                createBy: "",
                idoCode: "",
                odoId: "",
                odoCode: "",
            }
        }
    },
    methods: {
        //时间格式化
        creatFormatter(row) {
            if (row.createTime !== null) {
                return row.createTime.replace("T", " ");
            }
        },
        approveFormatter(row) {
            if (row.approverTime !== null) {
                return row.approverTime.replace("T", " ");
            }
        },

        //制单日期
        chooseTime() {
            this.search.startTime = this.createTime[0];
            this.search.endTime = this.createTime[1];
        },

        //审核日期
        applyChooseTime() {
            this.search.appStartTime = this.appTime[0];
            this.search.appEndTime = this.appTime[1];
        },

        //每页条数变化
        handleSizeChange(pageSize) {
            this.pageSize = pageSize;
            this.search.currentPage = this.currentPage;
            this.search.pageSize = pageSize;
            this.find(this.search);
        },
        //页码改变
        handleCurrentChange(currentPage) {
            this.currentPage = currentPage;
            this.search.currentPage = currentPage;
            this.search.pageSize = this.pageSize;
            this.find(this.search);
        },

        //重置
        resetForm() {
            this.search = {
                currentPage: 1,
                pageSize: 5,

                idoCode: "",//返厂入库单号
                createBy: "",//制单人
                status: "",//返厂出入库标志，0 未入库， 1 已入库
                approveStatus: "",//审核标志，0 审核不通过；1 未审核；2 已审核

                //创建时间
                startTime: "",
                endTime: "",

                //审核时间
                appStartTime: "",
                appEndTime: "",
            };
            //日期组件
            this.createTime = [];
            this.appTime = [];
            this.find();
        },

        //查询申请单
        find(params) {
            let vm = this;
            if (params == null) {
                params = {
                    currentPage: vm.$data.currentPage,
                    pageSize: vm.$data.pageSize,
                }
            }
            findInApply(params).then(value => {
                vm.applyList = value.records;
                vm.total = value.total;
            })
        },

        //搜索
        findPage() {
            this.find(this.search);
        },

        //查询已审核
        searchApproved() {
            this.search.approveStatus = 2;
            this.find(this.search);
        },
        //查询未审核
        searchToApprove() {
            this.search.approveStatus = 1;
            this.find(this.search);
        },
        //查询未通过
        searchReject() {
            this.search.approveStatus = 0;
            this.find(this.search);
        },

        //获得对应的商品信息进行审核
        showInfo(row) {
            console.log(row);
            this.dialogTableVisible = true;
            this.type = 0;
            this.approveInfo.id = row.id;
            this.approveInfo.odoId = row.odoId;
            this.approveInfo.odoCode = row.odoCode;
            this.createBy = row.createBy;
            this.idoCode = row.idoCode;
            this.approverComments = "";
            this.comments = row.comments;
            this.refacLocation = row.refacLocation;
            getInGoodWithInApplyId(row.id).then(value => {
                this.goodList = value;
            })
        },

        //审核跟踪
        showApprove(row) {
            this.dialogTableVisible = true;
            this.type = 1;
            this.comments = row.comments;
            this.refacLocation = row.refacLocation;
            this.approver = row.approver;
            this.approverTime = row.approverTime;
            this.approverComments = row.approverComments;
            this.approveStatus = row.approveStatus;
            this.idoCode = row.idoCode;
            if (row.approverTime !== null) {
                this.formatApproveTime = this.approverTime.replace("T", " ");
            }
            getInGoodWithInApplyId(row.id).then(value => {
                this.goodList = value;
            })
        },

        //审核通过
        pass() {
            this.approveInfo.createBy = this.createBy;
            this.approveInfo.idoCode = this.idoCode;
            this.approveInfo.approverComments = this.approverComments;
            this.approveInfo.approveStatus = 2;
            approveInApply(this.approveInfo).then(value => {
                this.$message.success(value);
                this.dialogTableVisible = false;
                this.find();
            });
        },

        //审核不通过
        reject() {
            this.approveInfo.approverComments = this.approverComments;
            this.approveInfo.approveStatus = 0;
            approveInApply(this.approveInfo).then(value => {
                this.$message.success(value);
                this.dialogTableVisible = false;
                this.find();
            });
        },
    },
    created() {
        this.find();
    }
};

export default inApplyApprove;